package com.arm.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.arm.common.utils.StringUtils;
import com.arm.system.domain.ArmInventory;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.arm.common.annotation.Log;
import com.arm.common.core.controller.BaseController;
import com.arm.common.core.domain.AjaxResult;
import com.arm.common.enums.BusinessType;
import com.arm.system.service.IArmInventoryService;
import com.arm.common.utils.poi.ExcelUtil;
import com.arm.common.core.page.TableDataInfo;

/**
 * 库存管理模块Controller
 * 
 * @author arm
 * @date 2023-11-06
 */
@RestController
@RequestMapping("/system/inventory")
public class ArmInventoryController extends BaseController
{
    @Autowired
    private IArmInventoryService armInventoryService;

    /**
     * 查询库存管理模块列表
     */
    @PreAuthorize("@ss.hasPermi('system:inventory:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArmInventory armInventory)
    {
        startPage();
        List<ArmInventory> list = armInventoryService.selectArmInventoryList(armInventory);
        return getDataTable(list);
    }

    /**
     * 导出库存管理模块列表
     */
    @PreAuthorize("@ss.hasPermi('system:inventory:export')")
    @Log(title = "库存管理模块", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArmInventory armInventory)
    {
        List<ArmInventory> list = armInventoryService.selectArmInventoryList(armInventory);
        ExcelUtil<ArmInventory> util = new ExcelUtil<ArmInventory>(ArmInventory.class);
        util.exportExcel(response, list, "库存管理模块数据");
    }

    /**
     * 获取库存管理模块详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:inventory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(armInventoryService.selectArmInventoryById(id));
    }

    /**
     * 新增库存管理模块
     */
    @PreAuthorize("@ss.hasPermi('system:inventory:add')")
    @Log(title = "库存管理模块", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArmInventory armInventory)
    {
        // 如果没有输入数量，默认库存数为0
        if (StringUtils.isBlank(armInventory.getNumber())){
            armInventory.setNumber("0");
        }
        return toAjax(armInventoryService.insertArmInventory(armInventory));
    }

    /**
     * 修改库存管理模块
     */
    @PreAuthorize("@ss.hasPermi('system:inventory:edit')")
    @Log(title = "库存管理模块", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArmInventory armInventory)
    {
        return toAjax(armInventoryService.updateArmInventory(armInventory));
    }

    /**
     * 删除库存管理模块
     */
    @PreAuthorize("@ss.hasPermi('system:inventory:remove')")
    @Log(title = "库存管理模块", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (Long id : ids) {
            if (armInventoryService.checkInventoryUnique(id))
            {
                return error("当前库存未清空，无法删除");
            }
        }
        return toAjax(armInventoryService.deleteArmInventoryByIds(ids));
    }
}
